package work.mediway.bi.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mediway.hos.app.core.util.SecurityUtils;
import com.mediway.hos.common.exception.GlobalException;
import org.springframework.stereotype.Service;
import work.mediway.bi.constant.BiDesignConstant;
import work.mediway.bi.mapper.DatasetConfigMapper;
import work.mediway.bi.model.dto.DataSetConfigDTO;
import work.mediway.bi.model.entity.DatasetConfig;
import work.mediway.bi.model.vo.DataSetConfigVO;
import work.mediway.bi.service.DatasetConfigService;

import java.util.List;

/**
  *@version
  *@description      
  *@author          bovane <bovane.ch@gmail.com>
  *@create          2023-07-10 09:59
  */  

@Service
public class DatasetConfigServiceImpl extends ServiceImpl<DatasetConfigMapper, DatasetConfig> implements DatasetConfigService{

    @Override
    public IPage<DataSetConfigVO> getDataSetConfigInfo(DataSetConfigDTO dataSetConfigDTO) {
        IPage<DatasetConfig> datasetConfigIPage = new Page<>(dataSetConfigDTO.getCurrent(), dataSetConfigDTO.getSize());
        List<DatasetConfig> datasetConfigs = this.baseMapper.selectDataSetConfig(datasetConfigIPage,dataSetConfigDTO);

        IPage<DataSetConfigVO> page = new Page<>(dataSetConfigDTO.getCurrent(),dataSetConfigDTO.getSize());
        List<DataSetConfigVO> dataSetConfigVOS = DataSetConfigVO.convert(datasetConfigs);
        page.setRecords(dataSetConfigVOS);
        return page;
    }

    @Override
    public Boolean addDatasetConfig(DatasetConfig datasetConfig) {
        if (ObjectUtil.isEmpty(datasetConfig.getActived())) {
            datasetConfig.setActived(Boolean.TRUE);
        }
        // 查看ID是否重复
        DatasetConfig one = this.getOne(new LambdaQueryWrapper<DatasetConfig>()
                .eq(DatasetConfig::getApiId, datasetConfig.getApiId()));
        if (ObjectUtil.isNotEmpty(one)) {
            throw new GlobalException(String.format("当前数据集配置ID已经存在【{}】", datasetConfig.getApiId()));
        }
        datasetConfig.setCreateBy(SecurityUtils.getLoginUser().getUserName());
        datasetConfig.setCreateTime(DateUtil.date());
        return this.save(datasetConfig);
    }

    @Override
    public Boolean updateDatasetConfig(DatasetConfig datasetConfig) {
        DatasetConfig one = this.getById(datasetConfig.getApiId());
        if (ObjectUtil.isEmpty(one)) {
            throw new GlobalException(String.format("更新的数据集配置ID不存在【{}】", datasetConfig.getApiId()));
        }
        datasetConfig.setUpdateBy(SecurityUtils.getLoginUser().getUserName());
        datasetConfig.setUpdateTime(DateUtil.date());
        return this.updateById(datasetConfig);
    }

    @Override
    public void deleteDatasetConfig(String ids) {
        List<String> id = StrUtil.split(ids, BiDesignConstant.DEFAULT_SPLIT);
        this.removeBatchByIds(id);
    }
}
